home *** CD-ROM | disk | FTP | other *** search
/ Champak 145 / (Vol 145) Dec 21 2011.iso / Games / kupidon-strike.swf / scripts / __Packages / mochi / MochiServices.as < prev   
Encoding:
Text File  |  2011-12-21  |  14.0 KB  |  414 lines

  1. class mochi.MochiServices
  2. {
  3.    var isLoading;
  4.    var startTime;
  5.    var waitInterval;
  6.    var _callbacks;
  7.    var onEnterFrame;
  8.    static var clip;
  9.    static var _id;
  10.    static var _container;
  11.    static var _clip;
  12.    static var _sendChannelName;
  13.    static var onError;
  14.    static var _listenChannel;
  15.    static var _loader;
  16.    static var _loaderListener;
  17.    static var _sendChannel;
  18.    static var _servicesURL = "http://www.mochiads.com/static/lib/services/services.swf";
  19.    static var _listenChannelName = "__ms_";
  20.    static var _connecting = false;
  21.    static var _connected = false;
  22.    static var netup = true;
  23.    static var netupAttempted = false;
  24.    function MochiServices()
  25.    {
  26.    }
  27.    static function get id()
  28.    {
  29.       return mochi.MochiServices._id;
  30.    }
  31.    static function get clip()
  32.    {
  33.       return mochi.MochiServices._container;
  34.    }
  35.    static function get childClip()
  36.    {
  37.       return mochi.MochiServices._clip;
  38.    }
  39.    static function getVersion()
  40.    {
  41.       return "1.43";
  42.    }
  43.    static function allowDomains(server)
  44.    {
  45.       var _loc1_ = server.split("/")[2].split(":")[0];
  46.       if(System.security)
  47.       {
  48.          if(System.security.allowDomain)
  49.          {
  50.             System.security.allowDomain("*");
  51.             System.security.allowDomain(_loc1_);
  52.          }
  53.          if(System.security.allowInsecureDomain)
  54.          {
  55.             System.security.allowInsecureDomain("*");
  56.             System.security.allowInsecureDomain(_loc1_);
  57.          }
  58.       }
  59.       return _loc1_;
  60.    }
  61.    static function get isNetworkAvailable()
  62.    {
  63.       if(System.security)
  64.       {
  65.          var _loc1_ = System.security;
  66.          if(_loc1_.sandboxType == "localWithFile")
  67.          {
  68.             return false;
  69.          }
  70.       }
  71.       return true;
  72.    }
  73.    static function set comChannelName(val)
  74.    {
  75.       if(val != undefined)
  76.       {
  77.          if(val.length > 3)
  78.          {
  79.             mochi.MochiServices._sendChannelName = val + "_fromgame";
  80.             mochi.MochiServices.initComChannels();
  81.          }
  82.       }
  83.    }
  84.    static function get connected()
  85.    {
  86.       return mochi.MochiServices._connected;
  87.    }
  88.    static function connect(id, clip, onError)
  89.    {
  90.       if(!mochi.MochiServices._connected && mochi.MochiServices._clip == undefined)
  91.       {
  92.          trace("MochiServices Connecting...");
  93.          mochi.MochiServices._connecting = true;
  94.          mochi.MochiServices.init(id,clip);
  95.       }
  96.       if(onError != undefined)
  97.       {
  98.          mochi.MochiServices.onError = onError;
  99.       }
  100.       else if(mochi.MochiServices.onError == undefined)
  101.       {
  102.          mochi.MochiServices.onError = function(errorCode)
  103.          {
  104.             trace(errorCode);
  105.          };
  106.       }
  107.    }
  108.    static function disconnect()
  109.    {
  110.       if(mochi.MochiServices._connected || mochi.MochiServices._connecting)
  111.       {
  112.          mochi.MochiServices._connecting = mochi.MochiServices._connected = false;
  113.          mochi.MochiServices.flush(true);
  114.          if(mochi.MochiServices._clip != undefined)
  115.          {
  116.             mochi.MochiServices._clip.removeMovieClip();
  117.             delete mochi.MochiServices._clip;
  118.          }
  119.          mochi.MochiServices._listenChannel.close();
  120.       }
  121.    }
  122.    static function init(id, clip)
  123.    {
  124.       mochi.MochiServices._id = id;
  125.       if(clip != undefined)
  126.       {
  127.          mochi.MochiServices._container = clip;
  128.       }
  129.       else
  130.       {
  131.          mochi.MochiServices._container = _root;
  132.       }
  133.       mochi.MochiServices.loadCommunicator(id,mochi.MochiServices._container);
  134.    }
  135.    static function loadCommunicator(id, clip)
  136.    {
  137.       var _loc3_ = "_mochiservices_com_" + id;
  138.       if(mochi.MochiServices._clip != null)
  139.       {
  140.          return mochi.MochiServices._clip;
  141.       }
  142.       if(!mochi.MochiServices.isNetworkAvailable)
  143.       {
  144.          return null;
  145.       }
  146.       if(mochi.MochiServices.urlOptions().servicesURL != undefined)
  147.       {
  148.          mochi.MochiServices._servicesURL = mochi.MochiServices.urlOptions().servicesURL;
  149.       }
  150.       mochi.MochiServices.allowDomains(mochi.MochiServices._servicesURL);
  151.       mochi.MochiServices._clip = clip.createEmptyMovieClip(_loc3_,10336,false);
  152.       mochi.MochiServices._listenChannelName += Math.floor(new Date().getTime()) + "_" + Math.floor(Math.random() * 99999);
  153.       mochi.MochiServices._loader = new MovieClipLoader();
  154.       if(mochi.MochiServices._loaderListener.waitInterval != null)
  155.       {
  156.          clearInterval(mochi.MochiServices._loaderListener.waitInterval);
  157.       }
  158.       mochi.MochiServices._loaderListener = {};
  159.       mochi.MochiServices._loaderListener.onLoadError = function(target_mc, errorCode, httpStatus)
  160.       {
  161.          trace("MochiServices could not load.");
  162.          mochi.MochiServices.disconnect();
  163.          mochi.MochiServices.onError.apply(null,[errorCode]);
  164.       };
  165.       mochi.MochiServices._loaderListener.onLoadStart = function(target_mc)
  166.       {
  167.          this.isLoading = true;
  168.       };
  169.       mochi.MochiServices._loaderListener.startTime = getTimer();
  170.       mochi.MochiServices._loaderListener.wait = function()
  171.       {
  172.          if(getTimer() - this.startTime > 10000)
  173.          {
  174.             if(!this.isLoading)
  175.             {
  176.                mochi.MochiServices.disconnect();
  177.                mochi.MochiServices.onError.apply(null,["IOError"]);
  178.             }
  179.             clearInterval(this.waitInterval);
  180.          }
  181.       };
  182.       mochi.MochiServices._loaderListener.waitInterval = setInterval(mochi.MochiServices._loaderListener,"wait",1000);
  183.       mochi.MochiServices._loader.addListener(mochi.MochiServices._loaderListener);
  184.       mochi.MochiServices._loader.loadClip(mochi.MochiServices._servicesURL + "?listenLC=" + mochi.MochiServices._listenChannelName + "&mochiad_options=" + escape(_root.mochiad_options),mochi.MochiServices._clip);
  185.       mochi.MochiServices._sendChannel = new LocalConnection();
  186.       mochi.MochiServices._sendChannel._queue = [];
  187.       mochi.MochiServices.listen();
  188.       return mochi.MochiServices._clip;
  189.    }
  190.    static function onStatus(infoObject)
  191.    {
  192.       var _loc0_ = null;
  193.       if((_loc0_ = infoObject.level) === "error")
  194.       {
  195.          mochi.MochiServices._connected = false;
  196.          mochi.MochiServices._listenChannel.connect(mochi.MochiServices._listenChannelName);
  197.       }
  198.    }
  199.    static function listen()
  200.    {
  201.       mochi.MochiServices._listenChannel = new LocalConnection();
  202.       mochi.MochiServices._listenChannel.handshake = function(args)
  203.       {
  204.          mochi.MochiServices.comChannelName = args.newChannel;
  205.       };
  206.       mochi.MochiServices._listenChannel.allowDomain = function(d)
  207.       {
  208.          return true;
  209.       };
  210.       mochi.MochiServices._listenChannel.allowInsecureDomain = mochi.MochiServices._listenChannel.allowDomain;
  211.       mochi.MochiServices._listenChannel._nextcallbackID = 0;
  212.       mochi.MochiServices._listenChannel._callbacks = {};
  213.       mochi.MochiServices._listenChannel.connect(mochi.MochiServices._listenChannelName);
  214.       trace("Waiting for MochiAds services to connect...");
  215.    }
  216.    static function initComChannels()
  217.    {
  218.       if(!mochi.MochiServices._connected)
  219.       {
  220.          mochi.MochiServices._sendChannel.onStatus = function(infoObject)
  221.          {
  222.             mochi.MochiServices.onStatus(infoObject);
  223.          };
  224.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:"handshakeDone"});
  225.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:"registerGame",id:mochi.MochiServices._id,clip:mochi.MochiServices._clip,version:getVersion()});
  226.          mochi.MochiServices._listenChannel.onStatus = function(infoObject)
  227.          {
  228.             mochi.MochiServices.onStatus(infoObject);
  229.          };
  230.          mochi.MochiServices._listenChannel.onReceive = function(pkg)
  231.          {
  232.             var _loc5_ = pkg.callbackID;
  233.             var _loc4_ = this._callbacks[_loc5_];
  234.             if(!_loc4_)
  235.             {
  236.                return undefined;
  237.             }
  238.             var _loc2_ = _loc4_.callbackMethod;
  239.             var _loc3_ = _loc4_.callbackObject;
  240.             if(_loc3_ && typeof _loc2_ == "string")
  241.             {
  242.                _loc2_ = _loc3_[_loc2_];
  243.             }
  244.             if(_loc2_ != undefined)
  245.             {
  246.                _loc2_.apply(_loc3_,pkg.args);
  247.             }
  248.             delete this._callbacks[_loc5_];
  249.          };
  250.          mochi.MochiServices._listenChannel.onError = function()
  251.          {
  252.             mochi.MochiServices.onError.apply(null,["IOError"]);
  253.          };
  254.          trace("connected!");
  255.          mochi.MochiServices._connecting = false;
  256.          mochi.MochiServices._connected = true;
  257.          while(mochi.MochiServices._sendChannel._queue.length > 0)
  258.          {
  259.             mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",mochi.MochiServices._sendChannel._queue.shift());
  260.          }
  261.       }
  262.    }
  263.    static function flush(error)
  264.    {
  265.       var _loc1_ = undefined;
  266.       var _loc2_ = undefined;
  267.       while(mochi.MochiServices._sendChannel._queue.length > 0)
  268.       {
  269.          _loc1_ = mochi.MochiServices._sendChannel._queue.shift();
  270.          false;
  271.          if(_loc1_.callbackID != null)
  272.          {
  273.             _loc2_ = mochi.MochiServices._listenChannel._callbacks[_loc1_.callbackID];
  274.          }
  275.          delete mochi.MochiServices._listenChannel._callbacks[_loc1_.callbackID];
  276.          if(error)
  277.          {
  278.             mochi.MochiServices.handleError(_loc1_.args,_loc2_.callbackObject,_loc2_.callbackMethod);
  279.          }
  280.       }
  281.    }
  282.    static function handleError(args, callbackObject, callbackMethod)
  283.    {
  284.       if(args != null)
  285.       {
  286.          if(args.onError != null)
  287.          {
  288.             args.onError.apply(null,["NotConnected"]);
  289.          }
  290.          if(args.options != null && args.options.onError != null)
  291.          {
  292.             args.options.onError.apply(null,["NotConnected"]);
  293.          }
  294.       }
  295.       if(callbackMethod != null)
  296.       {
  297.          args = {};
  298.          args.error = true;
  299.          args.errorCode = "NotConnected";
  300.          if(callbackObject != null && typeof callbackMethod == "string")
  301.          {
  302.             callbackObject[callbackMethod](args);
  303.          }
  304.          else if(callbackMethod != null)
  305.          {
  306.             callbackMethod.apply(args);
  307.          }
  308.       }
  309.    }
  310.    static function send(methodName, args, callbackObject, callbackMethod)
  311.    {
  312.       if(mochi.MochiServices._connected)
  313.       {
  314.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:methodName,args:args,callbackID:mochi.MochiServices._listenChannel._nextcallbackID});
  315.       }
  316.       else
  317.       {
  318.          if(mochi.MochiServices._clip == undefined || !mochi.MochiServices._connecting)
  319.          {
  320.             mochi.MochiServices.onError.apply(null,["NotConnected"]);
  321.             mochi.MochiServices.handleError(args,callbackObject,callbackMethod);
  322.             mochi.MochiServices.flush(true);
  323.             return undefined;
  324.          }
  325.          mochi.MochiServices._sendChannel._queue.push({methodName:methodName,args:args,callbackID:mochi.MochiServices._listenChannel._nextcallbackID});
  326.       }
  327.       mochi.MochiServices._listenChannel._callbacks[mochi.MochiServices._listenChannel._nextcallbackID] = {callbackObject:callbackObject,callbackMethod:callbackMethod};
  328.       mochi.MochiServices._listenChannel._nextcallbackID = mochi.MochiServices._listenChannel._nextcallbackID + 1;
  329.    }
  330.    static function urlOptions()
  331.    {
  332.       var _loc5_ = {};
  333.       if(_root.mochiad_options)
  334.       {
  335.          var _loc4_ = _root.mochiad_options.split("&");
  336.          var _loc2_ = 0;
  337.          while(_loc2_ < _loc4_.length)
  338.          {
  339.             var _loc3_ = _loc4_[_loc2_].split("=");
  340.             _loc5_[unescape(_loc3_[0])] = unescape(_loc3_[1]);
  341.             _loc2_ = _loc2_ + 1;
  342.          }
  343.       }
  344.       return _loc5_;
  345.    }
  346.    static function addLinkEvent(url, burl, btn, onClick)
  347.    {
  348.       var timeout = 1500;
  349.       var t0 = getTimer();
  350.       var _loc2_ = new Object();
  351.       _loc2_.mav = getVersion();
  352.       _loc2_.swfv = btn.getSWFVersion() || 6;
  353.       _loc2_.swfurl = btn._url;
  354.       _loc2_.fv = System.capabilities.version;
  355.       _loc2_.os = System.capabilities.os;
  356.       _loc2_.lang = System.capabilities.language;
  357.       _loc2_.scres = System.capabilities.screenResolutionX + "x" + System.capabilities.screenResolutionY;
  358.       var s = "?";
  359.       var _loc3_ = 0;
  360.       for(var _loc6_ in _loc2_)
  361.       {
  362.          if(_loc3_ != 0)
  363.          {
  364.             s += "&";
  365.          }
  366.          _loc3_ = _loc3_ + 1;
  367.          s = s + _loc6_ + "=" + escape(_loc2_[_loc6_]);
  368.       }
  369.       if(!(mochi.MochiServices.netupAttempted || mochi.MochiServices._connected))
  370.       {
  371.          var ping = btn.createEmptyMovieClip("ping",777);
  372.          var _loc7_ = btn.createEmptyMovieClip("nettest",778);
  373.          mochi.MochiServices.netupAttempted = true;
  374.          ping.loadMovie("http://x.mochiads.com/linkping.swf?t=" + getTimer());
  375.          _loc7_.onEnterFrame = function()
  376.          {
  377.             if(ping._totalframes > 0 && ping._totalframes == ping._framesloaded)
  378.             {
  379.                delete this.onEnterFrame;
  380.             }
  381.             else if(getTimer() - t0 > timeout)
  382.             {
  383.                delete this.onEnterFrame;
  384.                mochi.MochiServices.netup = false;
  385.             }
  386.          };
  387.       }
  388.       var _loc4_ = btn.createEmptyMovieClip("clk",1001);
  389.       _loc4_._alpha = 0;
  390.       _loc4_.beginFill(1044735);
  391.       _loc4_.moveTo(0,0);
  392.       _loc4_.lineTo(0,btn._height);
  393.       _loc4_.lineTo(btn._width,btn._height);
  394.       _loc4_.lineTo(btn._width,0);
  395.       _loc4_.lineTo(0,0);
  396.       _loc4_.endFill();
  397.       _loc4_.onRelease = function()
  398.       {
  399.          if(mochi.MochiServices.netup)
  400.          {
  401.             getURL(url + s,"_blank");
  402.          }
  403.          else
  404.          {
  405.             getURL(burl,"_blank");
  406.          }
  407.          if(onClick != undefined)
  408.          {
  409.             onClick();
  410.          }
  411.       };
  412.    }
  413. }
  414.